package com.hzhr.iot.user.convertor.custom;

import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.user.convertor.UserConvertor;
import com.hzhr.iot.user.entity.ext.*;
import com.hzhr.iot.user.model.user.UserModel;
import com.hzhr.iot.user.model.user_moreCompleteRegistInfo.User_MoreCompleteRegistInfoModel;
import com.hzhr.iot.user.model.user_registInfo.User_RegistInfoModel;
import com.hzhr.iot.user.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserCustomConvertor extends UserConvertor {
    @Autowired
    RoleCustomConvertor roleCustomConvertor;
    @Autowired
    RedisUtil redisUtil;

    @Override
    public UserModel ext2userModel(UserEntityExt in) {
        UserModel result = super.ext2userModel(in);

        CompanyEntityExt ext1 = in.getCompanyExt();
        if (ext1 != null) {
            result.setCompanyName(ext1.getName());
        }
        DeptEntityExt ext2 = in.getDeptExt();
        if (ext2 != null) {
            result.setDeptName(ext2.getName());
            result.setDeptCode(ext2.getCode());
        }
        List<RoleEntityExt> ext3 = in.getRoleExtList();
        if (ext3 != null) {
            result.setRoles(roleCustomConvertor.extList2roleModelList(ext3));
            if (ext3.size() > 0) {
                result.setFirstRoleId(ext3.get(0).getId());
                result.setFirstRoleName(ext3.get(0).getName());
            }
        }
        OptionsEntityExt ext4 = in.getGenderExt();
        if (ext4 != null) {
            result.setGenderDes(ext4.getDes());
        }
        return result;
    }

    @Override
    public User_MoreCompleteRegistInfoModel ext2user_MoreCompleteRegistInfoModel(UserEntityExt in) {
        User_MoreCompleteRegistInfoModel result = super.ext2user_MoreCompleteRegistInfoModel(in);

        String userName = in.getUserName();
        String rsToken = HzhrTypeUtil.object2string(redisUtil.get("rsToken:" + userName));
        result.setRsToken(rsToken);

        return result;
    }

    @Override
    public User_RegistInfoModel ext2user_RegistInfoModel(UserEntityExt in) {
        User_RegistInfoModel result = super.ext2user_RegistInfoModel(in);

        String userName = in.getUserName();
        String rsToken = HzhrTypeUtil.object2string(redisUtil.get("rsToken:" + userName));
        result.setRsToken(rsToken);

        return result;
    }
}

